package org.swust.monitor.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.swust.monitor.dto.SysUserDto;
import org.swust.monitor.entity.SysUser;
import org.swust.monitor.entity.qparam.QSysUser;
import org.swust.monitor.mapper.SysDeptMapper;
import org.swust.monitor.mapper.SysUserMapper;
import org.swust.monitor.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.swust.monitor.utils.SecurityUtils;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author 111
 * @since 2024-12-24
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Override
    public boolean save(SysUser entity) {
        boolean result = super.save(entity);
        if(result){
            deptMapper.saveDeptCount(entity.getDeptId(),1);
        }
        return result;
    }

//    @Override
//    public boolean removeById(SysUser entity) {
//        boolean result = super.removeById(entity);
//        if(result){
//            deptMapper.delDeptCount(entity.getDeptId(),-1);
//        }
//        return result;
//    }

//    public boolean update(SysUser entity) {
//        boolean result = updateById
//
//        return result;
//    }
    @Override
    public IPage<SysUserDto> pageUser(Page page, QSysUser qSysUser) {
        return userMapper.pageUser(page, qSysUser);
    }



    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
    @Override
    public boolean resetPwd(Integer userId) {
        String pwd= ENCODER.encode("123456");
        return baseMapper.resetPwd(userId, pwd) > 0;
    }
}
